﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TaDMS.Models;

namespace TaDMS.Controllers
{
    [Authorize(Roles = "Admin, Editer")]
    public class ApiEmployeeController : ApiController
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetAll(int start, int limit)
        {
            int? total;
            var employees = _unitOfWork.EmployeeRepository.GetData(out total, orderBy: order => order.OrderBy(v => v.FirstName).ThenBy(v => v.LastName), isPaging: true, start: start, limit: limit);

            if (String.IsNullOrEmpty(_unitOfWork.EmployeeRepository.ErrorMessage))
            {
                var enumerable = employees as IList<Employee> ?? employees.ToList();
                if (enumerable.Any() && total > 0)
                {
                    var result = new
                    {
                        total = total,
                        success = true,
                        message = "success",
                        data = enumerable
                    };
                    return Request.CreateResponse(HttpStatusCode.OK, result);
                }

                var empty = new
                {
                    success = false,
                    message = "Employees is empty"
                };
                return Request.CreateResponse(HttpStatusCode.OK, empty);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = _unitOfWork.EmployeeRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Create(Employee entity)
        {
            var result = _unitOfWork.EmployeeRepository.Create(entity);

            if (result != null && String.IsNullOrEmpty(_unitOfWork.EmployeeRepository.ErrorMessage))
            {
                var success = new
                {
                    data = result,
                    success = true,
                    message = "Created"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Craete false: " + _unitOfWork.EmployeeRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Update(Employee entity, string fields)
        {
            var result = _unitOfWork.EmployeeRepository.Update(entity, fields);

            if (result != null && String.IsNullOrEmpty(_unitOfWork.EmployeeRepository.ErrorMessage))
            {
                var success = new
                {
                    data = result,
                    success = true,
                    message = "Updated"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Update false: " + _unitOfWork.EmployeeRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Delete(Employee entity)
        {
            var result = _unitOfWork.EmployeeRepository.Delete(entity);

            if (result != null && String.IsNullOrEmpty(_unitOfWork.EmployeeRepository.ErrorMessage))
            {
                var success = new
                {
                    data = result,
                    success = true,
                    message = "Deleted"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Delete false: " + _unitOfWork.EmployeeRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }
    }
}